package com.customization.huangwm.workflowNodeAction.SW003;

import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Huangwm
 * @Date 2022-06-25
 * @Version 1.0
 * @Description TODO 物料領用節點后將明細表1分解到明細2，明細1中為資產的拆分成單條數據
 **/
public class AssetsApplicationStart implements Action {
    private LogTool log = new LogTool("/log/SW003/huangwm/WfAction/AssetsApplicationStart", false);
    private DBTools dbTools = new DBTools();
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String nodeType = requestInfo.getRequestManager().getNodetype();
        String src = requestInfo.getRequestManager().getSrc();
        int creater = requestInfo.getRequestManager().getCreater();
        log.writeLog("物料申領流程創建后分解>>>" + requestid + ">>" + nodeType + ">>" + src);
        String tableName = requestInfo.getRequestManager().getBillTableName();
        String billid = requestInfo.getRequestManager().getBillid() + "";

        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        String sql = "insert into "+tableName+"_dt2(mainid,assetCode,assetName,managerDep,isAsset,applicationQuantity,depManager,reviewQuantity,field1,userid)values(?,?,?,?,?,?,?,?,?,?)";
        List<List<Object>> insert = new ArrayList<>();
        rs.execute("delete from "+tableName+"_dt2 where mainid="+billid);
        //mainid,assetCode,assetName,managerDep,isAsset,applicationQuantity,depManager,reviewQuantity
        rs.execute("select * from "+tableName+"_dt1 where mainid="+billid);
        while (rs.next()){
            String assetCode = rs.getString("assetCode");
            String assetName = rs.getString("assetName");
            String managerDep = rs.getString("managerDep");
            String isAsset = rs.getString("isAsset");
            String field1 = rs.getString("field1");//仓库
            String userid = rs.getString("userid");//使用人
            int applicationQuantity = Double.valueOf(rs.getDouble("applicationQuantity")).intValue();
            String depManager = rs.getString("depManager");
            if(isAsset.equals("0")){
                //針對資產的物料一條條拆分插入
                for(int i=0;i<applicationQuantity;i++){
                    List<Object> data = new ArrayList<>();
                    data.add(billid);
                    data.add(assetCode);
                    data.add(assetName);
                    data.add(managerDep);
                    data.add(isAsset);
                    data.add(1);
                    data.add(depManager);
                    data.add(1);
                    data.add(field1);
                    data.add(userid);
                    insert.add(data);
                }
            }else{
                List<Object> data = new ArrayList<>();
                data.add(billid);
                data.add(assetCode);
                data.add(assetName);
                data.add(managerDep);
                data.add(isAsset);
                data.add(applicationQuantity);
                data.add(depManager);
                data.add(applicationQuantity);
                data.add(field1);
                data.add(userid);
                insert.add(data);
            }
        }
        try {
            log.writeLog(JSONObject.toJSONString(insert));
            rst.executeBatchSql(sql,insert);
        } catch (Exception e) {
            log.writeLog(e.getMessage());
            rst.rollback();
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestid);
            requestInfo.getRequestManager().setMessagecontent("複刻明細1插入到明細2執行SQL異常;"+e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }
}
